package cn.foodProject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.io.IOException;
import java.io.Serializable;

import cn.foodProject.R;
import cn.foodProject.adapter.CommentListAdapter;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.CommentGsonBean;
import okhttp3.Response;

//评论列表
public class CommentActivity extends AppCompatActivity implements Serializable,View.OnClickListener{
    private static final String TAG = "CommentActivity";
    private ListView listComment;
    private Toolbar toolbarCommentList;
    private CommentActivity context = CommentActivity.this;
    private Button btnAddComment;
    private int foodId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        initView();

        //获取上一个活动传来的foodId
        Intent intent = getIntent();
        foodId = intent.getIntExtra("foodId", -1);

        new Thread() {
            @Override
            public void run() {
                super.run();
                updateList();
            }
        }.start();
    }

    private void initView() {
        listComment = (ListView) findViewById(R.id.list_comment);
        toolbarCommentList = (Toolbar) findViewById(R.id.toolbar_comment_list);

        //toolbar相关
        setSupportActionBar(toolbarCommentList);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbarCommentList.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnAddComment = (Button) findViewById(R.id.btn_add_comment);
        btnAddComment.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add_comment:
                Intent intent = new Intent(context, AddCommentActivity.class);
                intent.putExtra("foodId", foodId);
                startActivity(intent);
                break;
        }
    }

    private void updateList() {
        try {
            Response response = MyOkhttp.getResponse(MyUrl.getUrl("/comment/findById/" + foodId));
            //当请求成功时
            if (response.isSuccessful()) {
                //使用gson解析返回的数据
                String s = response.body().string();
                final CommentGsonBean commentGsonBean = (CommentGsonBean) MyGson.fromJson(s, CommentGsonBean.class);
                final CommentListAdapter listCommentItemAdapter = new CommentListAdapter(context, commentGsonBean.getData());

                if (commentGsonBean.isSuccess()) {
                    //在主线程中更新listview并设置其点击事件
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listComment.setAdapter(listCommentItemAdapter);
                            listComment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(context, CommentDetailsActivity.class);
                                    intent.putExtra("id",commentGsonBean.getData().get(i).getId());
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                }
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "onFailure: 请求失败");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
